//
//  TrackImageView.m
//  student
//
//  Created by JCai on 2018/10/25.
//  Copyright © 2018 higer. All rights reserved.
//

#import "TrackImageView.h"
#import "UIImageView+WebCache.h"

@interface TrackImageView ()
{
    CGRect imageRect;
    CGFloat imageScale;
}

@property (nonatomic, strong) UIImage *originImage;

@end


@implementation TrackImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

+ (Class)layerClass{
    return [CATiledLayer class];
}

- (void)reloadPicture:(NSString *)filename
{
    NSString * urlString = [[NSString stringWithFormat:@"http://115.29.42.45:8088/higer_front_res/mapResources/sfkm2/1000/%@.png", filename] stringByReplacingOccurrencesOfString:@"8081" withString:@"8080"];
    NSURL *url = [NSURL URLWithString:urlString];
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    if ([manager diskImageExistsForURL:url]) {
        self.originImage = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString];
    } else {
        //从网络下载图片
        [manager downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
             //处理下载进度
         } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
             if (image) {
                 self.originImage = image;
             }
         }];
    }
    //NSString *path = [[NSBundle mainBundle] pathForResource:@"ExamPlayMap" ofType:@"jpg"];
    //self.originImage = [UIImage imageWithContentsOfFile:path];
    [self initSelf];
}

- (void)initSelf {
    imageRect = CGRectMake(0.0f, 0.0f,CGImageGetWidth(self.originImage.CGImage),CGImageGetHeight(self.originImage.CGImage));
    imageScale = self.frame.size.width/imageRect.size.width;
    imageScale = 0.5;
    CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
////
//    int lev = ceil(log2(1/imageScale))+1;
//    tiledLayer.levelsOfDetail = 1;
//    tiledLayer.levelsOfDetailBias = 1;
    tiledLayer.tileSize = CGSizeMake(128, 128);
////    if(self.tileCount>0){
////    CATiledLayer *tileLayer = (CATiledLayer *)self.layer;
//    CGSize tileSize = self.bounds.size;
//    NSInteger tileSizeScale = 1;
//    tileSize.width /=tileSizeScale;
//    tileSize.height/=tileSizeScale;
//    tiledLayer.tileSize = CGSizeMake(256.0f, 256.0f);
//    }
}

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    imageScale = self.frame.size.width/imageRect.size.width;
    CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
    
    //    int lev = ceil(log2(1/imageScale))+1;
//    tiledLayer.levelsOfDetail = 1;
//    tiledLayer.levelsOfDetailBias = 1;
    tiledLayer.tileSize = CGSizeMake(128, 128);
//    if(self.tileCount>0){
//        CATiledLayer *tileLayer = (CATiledLayer *)self.layer;
//        CGSize tileSize = self.bounds.size;
//        NSInteger tileSizeScale = 1;
//        tileSize.width /=tileSizeScale;
//        tileSize.height/=tileSizeScale;
//        tileLayer.tileSize = CGSizeMake(256.0f, 256.0f);
//    }
    
}
-(CGPoint)rectCenter:(CGRect)rect{
    CGFloat centerX = (CGRectGetMaxX(rect)+CGRectGetMinX(rect))/2;
    CGFloat centerY = (CGRectGetMaxY(rect)+CGRectGetMinY(rect))/2;
    
    return CGPointMake(centerX, centerY);
}

-(void)drawRect:(CGRect)rect {
    //将视图frame映射到实际图片的frame
    CGRect imageCutRect = CGRectMake(rect.origin.x / imageScale,rect.origin.y / imageScale,rect.size.width / imageScale,rect.size.height / imageScale);
    //截取指定图片区域，重绘
    @autoreleasepool{
        CGImageRef imageRef = CGImageCreateWithImageInRect(self.originImage.CGImage, imageCutRect);
        UIImage *tileImage = [UIImage imageWithCGImage:imageRef];
        CGContextRef context = UIGraphicsGetCurrentContext();
        UIGraphicsPushContext(context);
        [tileImage drawInRect:rect];
        UIGraphicsPopContext();
    }
}


-(CGSize)returnTileSize{
    return [(CATiledLayer*)self.layer tileSize];
}

@end
